package com.carck.service.impl;

import com.carck.config.ServerConfig;
import com.carck.mapper.ModelMapper;
import com.carck.pojo.Group;
import com.carck.pojo.Model;
import com.carck.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
@Service
public class ModelServiceImpl implements ModelService {
    @Autowired
    private ModelMapper modelMapper;

    @Override
    public ArrayList<Group> list() {
        ArrayList<Group> out=new ArrayList<>();
        ArrayList<String> groupNames=modelMapper.listGroupNames();
        for (String curGroupName : groupNames) {
            ArrayList<String> modelNames = modelMapper.getGroupModels(curGroupName);
            out.add(new Group(curGroupName, modelNames));
        }
        return out;
    }

    @Override
    public boolean add(String groupName, String modelName,String fileName) {
        if(modelMapper.isInModel(groupName,modelName)==0)
        {
            modelMapper.add(groupName,modelName,fileName);
        }
        else
        {
            return false;
        }
        return true;
    }

    @Override
    public String getFileURL(Model model) {
        String ipAddr= ServerConfig.getIPAdress();
        String port=ServerConfig.getPort();
        String serverAddrPort="http://"+ipAddr+":"+port+"/";
        return serverAddrPort+modelMapper.getFileURL(model)+".ply";
    }
}
